﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.DAL.DAOs;
using LibrarySystem.LibraryUtils.Entities;

namespace LibrarySystem.Controllers.LibraryControllers
{
    public class CategoryController : Controller
    {

        [HttpPost]
        public JsonResult CategoryOptions(string cate = null)
        {
            try
            {
                Library_Categories libCate = new Library_Categories();
                List<CategoryModel> cateModel = libCate.GetCateComboxList(cate);
                var cateOptions = cateModel.Select(s => new { Value = s.ID, DisplayText = s.CategoryName });

                return Json(new { Result = "OK", Options = cateOptions });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult GetCategoryList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            try
            {
                Library_Categories libCate = new Library_Categories();
                int count = libCate.GetDataCount();
                List<CategoryModel> cateList = libCate.GetDataList(jtStartIndex, jtPageSize, jtSorting);
                return Json(new { Result = "OK", Records = cateList, TotalRecordCount = count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult AddCategory(CategoryModel model)
        {
            try
            {
                Library_Categories libCate = new Library_Categories();
                var add = libCate.AddNewData(model);
                return Json(new { Result = "OK", Record = add });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult UpdateCategory(CategoryModel model)
        {
            try
            {
                Library_Categories libCate = new Library_Categories();
                libCate.UpdateData(model);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult DeleteCategory(int Id)
        {
            try
            {
                Library_Categories libCate = new Library_Categories();                
                libCate.DeleteData(Id);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

    }
}
